{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading tensorRt graph...\n",
      "Start time =  12:34:01\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: Logging before flag parsing goes to stderr.\n",
      "W0914 12:34:17.481374 548114849808 deprecation.py:323] From /home/jordan/Downloads/Ai Vehicle Pothole Detector (Powered by Jetson Nano Neural Computer)__________________/load_trt_graph.py:16: FastGFile.__init__ (from tensorflow.python.platform.gfile) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.gfile.GFile.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "image_size: [416, 416, 3]\n",
      "input_tensor_name: input_1:0\n",
      "output_tensor_name: lambda_2/Identity:0\n",
      "Loaded trt graph @ end time =  12:54:53\n"
     ]
    }
   ],
   "source": [
    "###################\n",
    "#Written by Jordan Bennett\n",
    "#Part B/4. Make predictions based on FP16 graph. \n",
    "#This is not expected to work on windows.\n",
    "#Expected to work on google colab or jetson nano like platform.\n",
    "###################\n",
    "\n",
    "###########\n",
    "#Section 1: Load TensorRT graph/saved model, as well as prediction and prediction decoder\n",
    "###########\n",
    "print (\"Loading tensorRt graph...\")\n",
    "\n",
    "from  datetime import datetime\n",
    "now = datetime.now ( )\n",
    "startTime = now.strftime(\"%H:%M:%S\")\n",
    "print ( \"Start time = \", startTime )\n",
    "\n",
    "\n",
    "from decode_hypothesis import decode_hypothesis\n",
    "import load_trt_graph as potholeGraph\n",
    "from tensorflow.keras.preprocessing import image\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "def getPrediction (image_path__):\n",
    "  img = image.load_img(image_path__, target_size=potholeGraph.image_size[:2]) #where image_size[:2] = \"[416,416,3]\", which corresponds somewhat to config[\"input_size\"] in config.json.\n",
    "  \n",
    "  x = image.img_to_array(img)/255.0 #CRUCIAL!!!-->jordan_normalize IMAGE_DATA=image.img_to_array(...) as seen in desktop version. Otherwise image data contains large integers, which is not expected by the trained pothole model which expects small normalized floating point values.\n",
    "  x = np.expand_dims(x, axis=0)\n",
    "\n",
    "  #x = preprocess_input(x) #irrelevant to hyppothesis accuracy\n",
    "  \n",
    "  feed_dict = {\n",
    "      potholeGraph.input_tensor_name: x\n",
    "  }\n",
    "  \n",
    "  hypothesis = potholeGraph.tf_sess.run(potholeGraph.output_tensor, feed_dict) \n",
    "\n",
    "  hypothesis = hypothesis.reshape ( 13, 13, 5, 6 ) #jordan_addition: correct network output shape based on observation of desktop output analysis\n",
    "\n",
    "  \n",
    "  #jordan_note: The output of the neural network is a bunch of pixels, or bounding boxes. Cardinality of those boxes equals pothole cardinality.\n",
    "  print('Caution!', len(decode_hypothesis(hypothesis)), 'pothole(s) are detected ahead from input image: ', image_path__ )\n",
    "\n",
    "\n",
    "then = datetime.now ( )\n",
    "endTime = then.strftime(\"%H:%M:%S\")\n",
    "print ( \"Loaded trt graph @ end time = \", endTime )\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "###########\n",
    "#Section 2: Do predictions based on loaded graph\n",
    "###########\n",
    "print (\"Begin smart pothole detection on 4 sample images...\")\n",
    "\n",
    "\n",
    "########################################################   \n",
    "########################################################   \n",
    "#####Test on image sample 0, with 8 potholes\n",
    "getPrediction ('pothole_sample_0.jpg')\n",
    "#####Test on image sample 1, with 8 potholes\n",
    "getPrediction ('pothole_sample_1.jpg')\n",
    "#####Test on image sample 2, with 8 potholes\n",
    "getPrediction ('pothole_sample_2.jpg')\n",
    "#####Test on image sample 3, with 0 potholes\n",
    "getPrediction ('pothole_negative_sample.jpg')\n",
    "\n",
    "\n",
    "\n",
    "###############################################\n",
    "#Runtime cost test. Test speed of prediction on optimized tensor rt graph \n",
    "#this same code is ran in Desktop version, which yielded (except for getPrediction which is swapped with desktop equivalent)\n",
    "print(\"\\n\\n########\\nExecution runtime cost test\")\n",
    "import time\n",
    "times = []\n",
    "for i in range(20):\n",
    "    start_time = time.time()\n",
    "    getPrediction ('pothole_sample_2.jpg')\n",
    "    delta = (time.time() - start_time)\n",
    "    times.append(delta)\n",
    "mean_delta = np.array(times).mean()\n",
    "fps = 1 / mean_delta\n",
    "print('average(sec):{:.2f},fps:{:.2f}'.format(mean_delta, fps))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
